#include "actionwidget.h"
#include "ui_actionwidget.h"
#include <QDebug>
#include "InnerWorkComponent/findwidget.h"
#include "InnerWorkComponent/friendmassager.h"
#include "InnerWorkComponent/scanwidget.h"
#include "myscanlineedit.h"
#include "widget.h"
#include "allnetscanwidget.h"
#include "friendrequestwidget.h"
#include "Core/tcpclient.h"
ActionWidget::ActionWidget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::ActionWidget)
{
    ui->setupUi(this);
    InitForUi();
    LinkUiToUiSignalAndSlot();
    LinkTcpSignalAndSlot();
}

ActionWidget::~ActionWidget()
{
    delete ui;
}

FindWidget *ActionWidget::GetFindWMsg()
{
    return FindWMsg;
}

FriendMassager *ActionWidget::GetFriendMsg()
{
    return FriendMsg;
}

void ActionWidget::setUserName(QString UserName)
{
    ui->UserName->setText(UserName);
}

void ActionWidget::InitForUi()
{
    QList<QWidget*> pages = ui->UserAction->findChildren<QWidget*>();
    for (QWidget* page : pages) {
        ui->UserAction->removeWidget(page);
        page->deleteLater();
    }
    AllNetScanWMsg=nullptr;
    FriendRequestMsg=nullptr;
    HideWidget=nullptr;

    ui->ScanBtn->setText("搜索");
    ui->FindBtn->setText("发现");

    FriendMsg=new FriendMassager;
    ChatWidget=new ChatListWidget;
    FindWMsg=new FindWidget;
    ScanWMsg=new ScanWidget;

    ui->UserAction->addWidget(ChatWidget);
    ui->UserAction->addWidget(FriendMsg);
    ui->UserAction->addWidget(FindWMsg);
    ui->UserAction->addWidget(ScanWMsg);
}

void ActionWidget::LinkUiToUiSignalAndSlot()
{
    connect(ui->FindBtn,&QPushButton::clicked,
            this,&ActionWidget::on_Find);

    connect(ui->ScanBtn,&QPushButton::clicked,
            this,&ActionWidget::on_Scan);

    connect(ui->HSearchEdit,&MyScanLineEdit::GetFocus,
            this,&ActionWidget::on_ScanLineFocus);

    connect(ui->HSearchEdit,&MyScanLineEdit::textEdited,
            this,&ActionWidget::LocalScanInfo);

}

void ActionWidget::LinkTcpSignalAndSlot()
{
}

void ActionWidget::on_InfoBtn_clicked()
{
    ui->UserAction->setCurrentIndex(0);
}

void ActionWidget::on_FriendBtn_clicked()
{
    ui->UserAction->setCurrentIndex(1);
}

void ActionWidget::on_Find()
{
    ui->UserAction->setCurrentIndex(2);
}

void ActionWidget::on_Scan()
{
    if(AllNetScanWMsg!=nullptr){
        AllNetScanWMsg->show();
        return;
    }
    AllNetScanWMsg=new AllNetScanWidget;
    AllNetScanWMsg->show();
}

void ActionWidget::on_ScanLineFocus()
{
    if(ui->UserAction->currentIndex()==3) return;
    ui->UserAction->setCurrentIndex(3);
}

void ActionWidget::LocalScanInfo()
{
    qDebug()<<ui->HSearchEdit->text();
}

